﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NewRetail.Application.Components.StoreValueCard.Model;
using NewRetail.Application.Components.StoreValueCard.Services;
using NewRetail.Application.Core;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NewRetail.Api.Controllers.StoreValueCardControllers
{
    [NewRetailRoute("storevalue-card")]
    public class StoreValueCardController : BaseController
    {
        #region Field
        public readonly IStoreValueCardService _storeValueCardService;
        private readonly ILogger _logger;
        #endregion

        #region Constructor
        public StoreValueCardController(IStoreValueCardService storeValueCardService, ILogger<StoreValueCardController> logger)
        {
            _storeValueCardService = storeValueCardService;
            _logger = logger;
        }
        #endregion

        #region method

        #region 储值卡相关

        #region 创建充值方案
        /// <summary>
        /// 创建充值方案
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("createrechargescheme")]
        public async Task<ResponseEntity<string>> CreateRechargeSchemeAsync([FromBody] RechargeSchemeRequestDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.CreateRechargeSchemeAsync(inputDto);
                if (result.Item1 == 0)
                    ret.SetError("创建失败！" + result.Item2);
                ret.SetData("创建成功");
            }
            catch (Exception e)
            {
                ret.SetError("创建失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 更新充值方案
        /// <summary>
        /// 更新充值方案
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("updaterechargescheme")]
        public async Task<ResponseEntity<string>> UpdateRechargeSchemeAsync([FromBody] UpdateRechargeSchemeRequestDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.UpdateRechargeSchemeAsync(inputDto);
                if (string.IsNullOrEmpty(result.Item1))
                    ret.SetError("更新失败！" + result.Item2);
                ret.SetData("更新成功");
            }
            catch (Exception e)
            {
                ret.SetError("更新失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 删除充值方案
        /// <summary>
        /// 删除充值方案
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>

        [HttpPost("deleterechargescheme")]
        public async Task<ResponseEntity<string>> DeleteRechargeSchemeAsync(string id)
        {
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.DeleteRechargeSchemeAsync(id);
                if (string.IsNullOrEmpty(result.Item1))
                    ret.SetError("删除失败！" + result.Item2);
                ret.SetData("删除成功");
            }
            catch (Exception e)
            {
                ret.SetError("删除失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取充值方案列表
        /// <summary>
        /// 获取充值方案列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpPost("searchrechargeschemes")]
        public async Task<ResponseEntity<IEnumerable<RechargeSchemeShowDto>>> SearchRechargeSchemes([FromBody] RechargeSchemeSearchDto searchDto)
        {
            var ret = new ResponseEntity<IEnumerable<RechargeSchemeShowDto>>();
            try
            {
                var result = await _storeValueCardService.GetRechargeSchemes(searchDto);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 根据Id获取充值方案
        /// <summary>
        /// 根据Id获取充值方案
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("searchrechargeschemebyid")]
        public async Task<ResponseEntity<RechargeSchemeShowDto>> SearchRechargeSchemeById(string id)
        {
            var ret = new ResponseEntity<RechargeSchemeShowDto>();
            try
            {
                var result = await _storeValueCardService.GetRechargeSchemeById(id);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取充值记录列表
        /// <summary>
        /// 获取充值记录列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpPost("searchrechargerecords")]
        public async Task<ResponseEntity<IEnumerable<RechargeRecordShowDto>>> SearchRechargeRecords([FromBody] RechargeRecordSearchDto searchDto)
        {
            var ret = new ResponseEntity<IEnumerable<RechargeRecordShowDto>>();
            try
            {
                var result = await _storeValueCardService.GetRechargeRecords(searchDto);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 更新充值记录订单状态(仅供手机端充值使用)
        /// <summary>
        /// 更新充值记录订单状态(仅供手机端充值使用)
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("updaterechargerecordstatus")]
        public async Task<ResponseEntity<string>> UpdateRechargeRecordStatusAsync([FromBody] RechargeRecordUpdateStatusDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.UpdateRechargeRecordStatus(inputDto);
                if (!result.Item1)
                    ret.SetError("更新失败！" + result.Item2);
                ret.SetData("更新成功");
            }
            catch (Exception e)
            {
                ret.SetError("更新失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 更新储值卡余额（仅供POS端使用）
        /// <summary>
        /// 更新储值卡余额（仅供POS端使用）
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("updaterechargerecordbalance")]
        public async Task<ResponseEntity<string>> UpdateRechargeRecordBalanceAsync([FromBody] RechargeRecordUpdateBalanceDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.UpdateRechargeRecordBalance(inputDto);
                if (!result.Item1)
                    ret.SetError("更新失败！" + result.Item2);
                else
                    ret.SetData("更新成功");
            }
            catch (Exception e)
            {
                ret.SetError("更新失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 储值卡余额使用-更新余额（仅供POS端使用）
        /// <summary>
        /// 储值卡余额使用-更新余额（仅供POS端使用）
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("updatecardbalancechange")]
        public async Task<ResponseEntity<string>> UpdateCardBalanceChangeAsync([FromBody] CardUpdateBalanceChangeDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.UpdateCardBalanceChange(inputDto);
                if (!result.Item1)
                    ret.SetError("更新失败！" + result.Item2);
                else
                    ret.SetData("更新成功");
            }
            catch (Exception e)
            {
                ret.SetError("更新失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 获取储值卡余额变化列表
        /// <summary>
        /// 获取储值卡余额变化列表
        /// </summary>
        /// <param name="searchDto"></param>
        /// <returns></returns>
        [HttpPost("searcardbalancechanges")]
        public async Task<ResponseEntity<IEnumerable<CardBalanceChangesShowDto>>> SearchCardBalanceChanges([FromBody] CardBalanceChangeSearchDto searchDto)
        {
            var ret = new ResponseEntity<IEnumerable<CardBalanceChangesShowDto>>();
            try
            {
                var result = await _storeValueCardService.GetCardBalanceChanges(searchDto);
                ret.SetData(result.Item1);
            }
            catch (Exception e)
            {
                ret.SetError("失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #region 储值卡充值并更新储值卡余额（仅供后台管理系统使用）
        /// <summary>
        /// 储值卡充值并更新储值卡余额（仅供后台管理系统使用）
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost("cardrecharge")]
        public async Task<ResponseEntity<string>> CardRechargeAsync([FromBody] RechargeRecordUpdateBalanceDto inputDto)
        {
            inputDto.Should().NotBeNull();
            var ret = new ResponseEntity<string>();
            try
            {
                var result = await _storeValueCardService.UpdateRechargeRecordBalance(inputDto, 1);
                if (!result.Item1)
                    ret.SetError("更新失败！" + result.Item2);
                else
                    ret.SetData("更新成功");
            }
            catch (Exception e)
            {
                ret.SetError("更新失败！" + e.Message);
                _logger.LogError(e.Message);
            }

            return ret;
        }
        #endregion

        #endregion

        #endregion
    }
}
